package com.sheepone.util.time;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author Miss.杨
 * @description
 * @since 2025/3/13 - 21:23
 */
public class TimeFormatter {
    private TimeFormatter() {
    }

    /**
     * TemporalAccessor 字符串格式化
     *
     * @param temporal
     * @param pattern
     * @return
     */
    public static String format(TemporalAccessor temporal, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(temporal);
    }

    /**
     * Date 字符串格式化
     *
     * @param dateStr
     * @param pattern
     * @return
     */
    public static String formatDate(Date dateStr, String pattern) {
        return new SimpleDateFormat(pattern).format(dateStr);
    }

    /**
     * LocalDateTime 字符串格式化
     *
     * @param time
     * @param pattern
     * @return
     */
    public static String formatLocalDateTime(LocalDateTime time, String pattern) {
        if (pattern == null) {
            return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(time);
        }
        return DateTimeFormatter.ofPattern(pattern).format(time);
    }

    /**
     * LocalDateTime 字符串格式化
     *
     * @param time
     * @return
     */
    public static String formatLocalDateTime(LocalDateTime time) {
        return formatLocalDateTime(time, null);
    }

    /**
     * LocalDate 字符串格式化
     *
     * @param time
     * @param pattern
     * @return
     */
    public static String formatLocalDate(LocalDate time, String pattern) {
        if (pattern == null) {
            return DateTimeFormatter.ISO_LOCAL_DATE.format(time);
        }
        return DateTimeFormatter.ofPattern(pattern).format(time);
    }

    /**
     * LocalDate 字符串格式化
     *
     * @param time
     * @return
     */
    public static String formatLocalDate(LocalDate time) {
        return formatLocalDate(time, null);
    }

    /**
     * LocalTime 字符串格式化
     *
     * @param timeStr
     * @return
     */
    public static String formatLocalTime(LocalTime timeStr) {
        return formatLocalTime(timeStr, null);
    }

    /**
     * LocalTime 字符串格式化
     *
     * @param timeStr
     * @param pattern
     * @return
     */
    public static String formatLocalTime(LocalTime timeStr, String pattern) {
        if (pattern == null) {
            return DateTimeFormatter.ISO_LOCAL_TIME.format(timeStr);
        }
        return DateTimeFormatter.ofPattern(pattern).format(timeStr);
    }

    /**
     * 时间戳 字符串格式化
     *
     * @param timeStamp
     * @param unit
     * @param pattern
     * @return
     */
    public static String formatTimeStamp(long timeStamp, TimeUnit unit, String pattern) {
        if (unit != TimeUnit.MILLISECONDS) {
            timeStamp = unit.toMillis(timeStamp);
        }
        Instant instant = Instant.ofEpochMilli(timeStamp);
        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return formatLocalDateTime(dateTime, pattern);
    }

    /**
     * 时间戳 字符串格式化
     *
     * @param timeStamp
     * @param pattern
     * @return
     */
    public static String formatTimeStamp(long timeStamp, String pattern) {
        return formatTimeStamp(timeStamp, TimeUnit.MILLISECONDS, pattern);
    }

    /**
     * 时间戳 字符串格式化
     *
     * @param timeStamp
     * @param unit
     * @return
     */
    public static String formatTimeStamp(long timeStamp, TimeUnit unit) {
        return formatTimeStamp(timeStamp, unit, null);
    }

    /**
     * 时间戳 字符串格式化
     *
     * @param timeStamp
     * @return
     */
    public static String formatTimeStamp(long timeStamp) {
        return formatTimeStamp(timeStamp, TimeUnit.MILLISECONDS, null);
    }

}
